package com.tanhua.app.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.api.*;
import com.tanhua.app.interceptor.UserThreadLocal;
import com.tanhua.model.pojo.Question;
import com.tanhua.model.pojo.Settings;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {


    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private BlackListApi blackListApi;

    public SettingsVo settings() {
        SettingsVo settingsVo = new SettingsVo();
        Long userId = UserThreadLocal.getid();
        settingsVo.setId(userId);
        String mobile = UserThreadLocal.getMobile();
        settingsVo.setPhone(mobile);
        Question question = questionApi.findById(userId);
        if (question == null) {
            settingsVo.setStrangerQuestion("你喜欢编程吗");
        } else {
            settingsVo.setStrangerQuestion(question.getTxt());
        }
        Settings settings = settingsApi.findById(userId);
        if (settings != null) {
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }
        return settingsVo;
    }


    public void notifications(Settings settings) {
        Long userId = UserThreadLocal.getid();
        Settings settings1 = settingsApi.findById(userId);
        settings.setUserId(userId);
        if (settings1 == null) {
            settingsApi.save(settings);
        } else {
            settings.setId(settings1.getId());
            settingsApi.update(settings);
        }
    }

    public void questions(Question question1) {
        Long userId = UserThreadLocal.getid();
        Question question = questionApi.findById(userId);
        question1.setUserId(userId);
        if (question == null) {
            questionApi.save(question1);
        } else {
            question1.setId(question.getId());
            questionApi.update(question1);
        }
    }

    public PageResult<UserInfo> blacklist(int page, int pageSize) {
        Page<UserInfo> page1 = new Page<UserInfo>(page, pageSize);
        Long userId = UserThreadLocal.getid();
        Page<UserInfo> page2 = userInfoApi.page(page1, userId);
        PageResult<UserInfo> result = new PageResult<>((int) page2.getTotal(), pageSize, (int) page2.getPages(), pageSize, page2.getRecords());
        return result;
    }

    public void delete(Long userId) {
        Long myUserId = UserThreadLocal.getid();
        blackListApi.delete(myUserId, userId);
    }
}
